package net.xiaoyu233.mitemod.miteite.trans.block;

import net.minecraft.*;
import net.xiaoyu233.mitemod.miteite.block.Blocks;
import net.xiaoyu233.mitemod.miteite.entity.EntityInfernalOverlord;
import net.xiaoyu233.mitemod.miteite.util.ParticleUtils;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(BlockPumpkin.class)
public class BlockPumpkinTrans  {

    @Inject(method = "onBlockAdded",at =@At("RETURN"),remap = false)
    private void onBlockPlaced(World par1World, int x, int y, int z, CallbackInfo ci){
        if ( checkVerticalPillar(par1World, x,y-1, z)) {
            moveblock(par1World, x,y, z);
            spawnBoss(par1World, x, y - 2, z);
        }
    }

    private boolean checkVerticalPillar(World world, int x, int startY, int z) {
        for (int dy = 0; dy < 3; dy++) {
            if (world.getBlock(x, startY - dy, z) != Blocks.blockVibranium) return false;
        }
        return true;
    }
    private void moveblock(World world, int x, int startY, int z){
        for (int dy = 0; dy < 4; dy++) {
            world.setBlock(x, startY - dy, z, 0, 0, 2);
        }
    }
    private void spawnBoss(World world, int x, int baseY, int z) {
        EntityInfernalOverlord boss = new EntityInfernalOverlord(world);
        boss.setPosition(x + 0.5, baseY + 1, z + 0.5);
        boss.refreshDespawnCounter(-9600);
        boss.entityFX(EnumEntityFX.summoned);
        boss.onSpawnWithEgg(null);
        world.spawnEntityInWorld(boss); // 1.6.4的生成实体方法


        // 特效：保留所有方块，仅播放粒子
        world.playSoundEffect(x + 0.5, baseY + 1, z + 0.5, "mob.enderdragon.growl", 5.0F, 0.8F);

        // 生成火焰粒子漩涡
        for (int i = 0; i < 100; i++) {
            double angle = i * Math.PI * 0.2;
            double radius = 3.0;
            double px = x + 0.5 + Math.cos(angle) * radius;
            double pz = z + 0.5 + Math.sin(angle) * radius;
            ParticleUtils.spawnParticle(world, EnumParticle.flame.ordinal(),
                    px, baseY + 1, pz, 0, 0.1, 0);
        }

        for (int dy = 0; dy < 4; dy++) {
            world.notifyBlockChange(x, baseY + 2 - dy, z ,0);
        }
    }
}
